﻿#ifndef SQLBASE_H
#define SQLBASE_H

#include <QObject>
#include <QTime>
#include <QMutex>
#include <QDate>
#include <QDateTime>

#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>

#include "myhelper.h"
#include "tables.h"
#include "config.h"
#include "globaldata.h"
#include "agreement.h"

class SqlBase : public QObject
{
    Q_OBJECT
public:
    explicit SqlBase(QObject *parent = nullptr);

signals:

public slots:
    void OnTimer();

    void Open();
    void Close();
    void CreateTable();
    void InitDefaultData();
    void StopAllAlarm();

    QSqlQuery Query(QString sql);
    bool ExecSql(QString sql);
    bool DeleteData(QString table, QString key, QString value);

    QString ToTime(QTime time);
    QString ToData(QDate date);
    QString ToDateTime(QDateTime dateTime);

    //用户
    bool SaveUser(User bean);
    bool DelUser(QString id);
    QList<User> GetUserList();

    //日志
    bool SaveSysLog(SysLog bean);
    bool DelSysLog(int id);
    bool DelSysLog(QDateTime dateTime);
    QList<SysLog> GetSysLogList(SysLog bean);

    //设备
    bool SaveEquipment(Equipment bean);
    bool UpdateEquipment(Equipment bean);
    bool DelEquipment(QString code);
    QList<Equipment> GetEquipmentList();
    QMap<QString, Equipment> GetEquipmentMap();
    //QMap<QString, Simulation> GetSimulationMap();

    //ip
    QList<IPData> GetIpList();
    bool SaveIPData(IPData bean);
    bool UpdateIPData(IPData bean);
    bool DelIPData(QString IP);
    QMap<QString, IPData> GetIpDataMap();

    //DM HYJ
    bool SaveAgreement(FrameFormat format);
    bool UpdateAgreement(FrameFormat bean);
    bool DelAgreement(FrameFormat code);
    QList<FrameFormat> GetAgreementList();

    //指标测量结果
    bool SaveQuality(Quality bean);
    bool DelQuality(long id);
    bool DelQuality(QDateTime dateTime);
    QList<Quality> GetQualityList(Quality bean);
    bool Savedata_State(State *state,QDateTime date);//保存状态量
    QList<State> GetStateList(QString name,QDateTime DATE1 , QDateTime DATE2);
    bool Deldata_State(QDateTime dateTime);

    bool Savedata_Sim(Simulation *sim,QDateTime data);
    QList<Simulation> GetSimList(QString name,QDateTime DATE1 , QDateTime DATE2);
    bool Deldata_Sim(QDateTime dateTime);


    //指标报警
    bool SaveQualityAlarm(QualityAlarm bean);
    bool UpdateQualityAlarmRelieve(QualityAlarm bean);
    bool DelQualityAlarm(long id);
    QList<QualityAlarm> GetQualityAlarmList(QualityAlarm bean);

public:
    int type;       //0 - sqlite 1-mysql

    GlobalData      *globalData;
    QTimer          *timer;
    QMutex          mutex;
    QSqlDatabase    dataBase; //QSqlDatabase提供访问数据库的链接
};

#endif // SQLBASE_H
